<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组方法3</title>
    <script>
        //reduce 方法对数组中的每个元素按序执行一个提供的 reducer 函数，
        //每一次运行 reducer 会将先前元素的计算结果作为参数传入，最后将其结果汇总为单个返回值
        const array1 = [1, 2, 3, 4];
        const initialValue = 0;
        const sumWithInitial = array1.reduce(
            (accumulator, currentValue) => accumulator + currentValue,
            initialValue,
        );
        console.log(sumWithInitial);

        //从后向前reduceRight返回每一个值
        const array2 = [
            [0, 1],
            [2, 3],
            [4, 5],
        ];
        const result = array2.reduceRight((accumulator, currentValue) =>
            accumulator.concat(currentValue),
        );
        console.log(result);
        let result2 = array2.reduceRight((currentValue) => console.log(currentValue))

        //reverse反转数组
        let arr1 = [1, 2, 3]
        console.log(arr1);
        arr1.reverse();
        console.log(arr1);

        //shift移除第一个元素，并将其返回
        let firstElement = arr1.shift();
        console.log(arr1);
        console.log(firstElement);

        //slice(2)：返回从下标2开始的元素
        //slice(2, 4):返回从下标2到4的元素（不包含4）
        //slice(-2)：返回从倒数第二个开始的到结束的元素
        //slice(2, -1):返回从下标2到倒数第一个元素的（不包含倒数第一个）
        //slice():将数组按字符分割开,几乎无变化
        const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
        console.log(animals.slice(2));
        console.log(animals.slice(2, 4));
        console.log(animals.slice(1, 5));
        console.log(animals.slice(-2));
        console.log(animals.slice(2, -1));
        console.log(animals.slice());

        //some存在符合函数条件的值
        const array = [1, 2, 3, 4, 5];
        const even = (element) => element % 2 === 0;
        console.log(array.some(even));

        //sort默认排序是将元素转换为字符串，然后按照它们的 UTF-16 码元值升序排序
        const months = ['March', 'Jan', 'Feb', 'Dec'];
        months.sort();
        console.log(months);
        const array3 = [1, 30, 4, 21, 100000];
        array3.sort();
        console.log(array3);

        //splice(1, 0, 'Feb')插入'Feb'到下标1
        //splice(4, 1, 'May')将下标4元素替换为'May'
        const months2 = ['Jan', 'March', 'April', 'June'];
        months2.splice(1, 0, 'Feb');
        console.log(months2);
        months2.splice(4, 1, 'May');
        console.log(months2);

        //toLocaleString() 方法返回一个字符串，表示数组中的所有元素。
        //每个元素通过调用它们自己的 toLocaleString 方法转换为字符串，
        //并且使用特定于语言环境的字符串（例如逗号“,”）分隔开
        const array5 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')];
        const localeString = array5.toLocaleString('en', { timeZone: 'UTC' });
        console.log(localeString);

        {
            // toReverse是返回一个元素顺序相反的新数组
            const items = [1, 2, 3];
            console.log(items); // [1, 2, 3]

            const reversedItems = items.toReversed();
            console.log(reversedItems); // [3, 2, 1]
            console.log(items); // [1, 2, 3]
        }

        {
            const months = ["Mar", "Jan", "Feb", "Dec"];
            const sortedMonths = months.toSorted();
            console.log(sortedMonths); // ['Dec', 'Feb', 'Jan', 'Mar']
            console.log(months); // ['Mar', 'Jan', 'Feb', 'Dec']

            const values = [1, 10, 21, 2];
            const sortedValues = values.toSorted((a, b) => a - b);
            console.log(sortedValues); // [1, 2, 10, 21]
            console.log(values); // [1, 10, 21, 2]
        }
        {
            const array1 = [1, 2, 3];
            console.log(array1.unshift(4, 5));
            console.log(array1);
        }
        {
            const array1 = [1, 2, 'a', '1a'];
            console.log(array1.toString());
        }
        {
            //values方法返回一个新的数组迭代器对象，该对象迭代数组中每个元素的值
            const array1 = ['a', 'b', 'c'];
            const iterator = array1.values();
            for (const value of iterator) {
                console.log(value);
            }
        }
        {
            //with返回一个新数组，其指定索引处的值会被新值替换
            const arr = [1, 2, 3, 4, 5];
            console.log(arr.with(2, 6)); // [1, 2, 6, 4, 5]
            console.log(arr); // [1, 2, 3, 4, 5]
            console.log(arr.with(2, 6).map((x) => x ** 2)); // [1, 4, 36, 16, 25]
        }
    </script>
</head>

<body>

</body>

</html>